package edu.uc.ui;

import com.foodview1.R;

import edu.uc.dto.User;
import edu.uc.persistence.LoginDAOInterface;
import edu.uc.persistence.LoginDAO;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
 * MainActivity class
 * 
 * @author Ckorczyk1
 * 
 * This class contains the coding for all of the buttons on the main Screen.
 *
 */
public class MainActivity extends Activity {

	/**
	 * onCreate method
	 * @param = Bundle SavedInstanceState
	 * 
	 */

	// Declare new Context
	public static Context context;
	private final String CLASS_NAME = getClass().getName();


	@Override
	public void onCreate(Bundle savedInstanceState) {

		// Appoint context to this activity
		context = this;

		setTitle("Food View");
		super.onCreate(savedInstanceState);

		// Declare new stub
		LoginDAOInterface LoginCheck;
		LoginCheck = new LoginDAO();

		// Create SessionUser global preference list
		final SharedPreferences SessionUserMgr = PreferenceManager.getDefaultSharedPreferences(context);

		// Initialize a new SessionUser object
		User SessionUser = new User();

		// Set the values of SessionUser object to reflect from global preference list
		SessionUser.setUserId(SessionUserMgr.getString("User_Id", ""));
		SessionUser.setFirstName(SessionUserMgr.getString("First_Name", ""));
		SessionUser.setLastName(SessionUserMgr.getString("Last_Name", ""));
		SessionUser.setEmail(SessionUserMgr.getString("Email", ""));
		SessionUser.setPassword(SessionUserMgr.getString("Password", ""));

		// Run login check
		try {

			Log.i(CLASS_NAME, "System checking if user is logged on...");
			LoginCheck.isLoggedIn(SessionUser);
		} catch (Exception e) {
			Log.e(CLASS_NAME, "There was a problem checking if the user was logged on!");
			e.printStackTrace();
		}

		// Set main content view
		setContentView(R.layout.activity_main);


		// Run login check to display buttons
		Button btnLogin;
		Button btnMyAcnt;

		btnLogin =  (Button) findViewById(R.id.btnLogin);
		btnMyAcnt = (Button) findViewById(R.id.btnMyAcnt);

		Log.d(CLASS_NAME, "Setting button visability...");

		if (SessionUser.getPassword().toString().equals("")) {
			btnLogin.setVisibility(View.VISIBLE);
			btnMyAcnt.setVisibility(View.INVISIBLE);

			Log.d(CLASS_NAME, "User not logged in! Display LOGIN button.");
		}

		else {
			btnLogin.setVisibility(View.INVISIBLE);
			btnMyAcnt.setVisibility(View.VISIBLE);

			Log.d(CLASS_NAME, "User is logged in! Display MY ACCOUNT button.");
		}

		// Set Main Screen welcome message based on SessionUser
		String FirstName = (SessionUser.getFirstName().toString());
		TextView WelcomeDisplay = (TextView)findViewById(R.id.WelcomeUserName);

		// Change text
		WelcomeDisplay.setText(" " + FirstName);


		//Create Search button object
		Button btnSearch = (Button)findViewById(R.id.btnSearch);
		// Register the onClick listener
		btnSearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v){
				Intent clickSearch = new Intent(v.getContext(), SearchActivity.class);
				startActivityForResult(clickSearch, 2);
				finish();

			}
		});

		// Register the onClick listener
		btnMyAcnt.setOnClickListener(new OnClickListener() {

			/**
			 * method onClick
			 * 
			 */
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent clickLogin = new Intent(v.getContext(), myReviewsActivity.class);
				startActivityForResult(clickLogin, 0);
				finish();
			}
		});

		// Register the onClick listener
		btnLogin.setOnClickListener(new OnClickListener() {

			/**
			 * method onClick
			 * 
			 */
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent clickLogin = new Intent(v.getContext(), loginActivity.class);
				startActivityForResult(clickLogin, 0);
				finish();
			}
		});

		// Create new NearbyRestaurant button object
		Button btnTakePhoto = (Button)findViewById(R.id.btnTakePhoto);
		// Register the onClick listener
		btnTakePhoto.setOnClickListener(new OnClickListener(){
			/**
			 * onClick method
			 * 
			 */
			@Override
			public void onClick(View v){
				Intent clickCamera = new Intent(v.getContext(), CameraActivity.class);
				startActivityForResult(clickCamera, 3);
				finish();
			}
		});



	}
	//variables for menu items
	public static final int SEARCH_RESTAURANTS = 1;
	public static final int REVIEW_MEAL = 2;
	public static final int NEARBY_RESTAURANTS = 3;
	public static final int SHOW_MAP = 4;
	public static final int MY_REVIEWS = 5;
	public static final int LOGOUT = 6;

	/**
	 * onCreateMethod
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);

		//		menu.add(Menu.NONE, SEARCH_RESTAURANTS, Menu.FIRST, "Search");
		//		menu.add(Menu.NONE, REVIEW_MEAL, Menu.FIRST+1, "Review Meal");
		//		menu.add(Menu.NONE, NEARBY_RESTAURANTS, Menu.FIRST+2, "Nearby Restaurants");
		//		menu.add(Menu.NONE, SHOW_MAP, Menu.FIRST+3, "Show Map");
		return true;
	}

	/**
	 * onOptionsItemSelected method
	 * 
	 * @param item
	 * 
	 * Method handles the the selected items on the menu that pops up 
	 * when the menu button is pressed.
	 * 
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		super.onOptionsItemSelected(item);

		//switch case pulls items from the menu main_activity.xml file
		//this will populate the menu on all screens.
		switch (item.getItemId()) {
		case R.id.SEARCH_RESTAURANTS:
			searchRestaurants();
			break;
		case R.id.SHOW_MAP:
			showMap();
			break;
		case R.id.MY_REVIEWS:
			myReviews();
			break;
		case R.id.MAIN:
			home();
			break;
		case R.id.REVIEW_FOOD:
			reviewFood();
			break;
		case R.id.LOGOUT_USER:
			logoutUser();
		default:
			// this is what happens if there is not a case that matches item.getItemId().
		}

		return true;


	}
	// method showMap - brings up the map screen to display nearby restaurants
	private void showMap() {
		Intent showMapRestaurants = new Intent(this, mapRestaurantActivity.class);
		startActivity(showMapRestaurants);

	}
	// method searchRestaurants - Brings up the search screen containing search criteria
	private void searchRestaurants() {
		Intent searchRestaurants = new Intent(this, SearchActivity.class);
		startActivity(searchRestaurants);

	}
	//method myReviews - brings up the screen with a list of all of the users previous reviews
	private void myReviews() {
		Intent myReviews = new Intent(this, myReviewsActivity.class);
		startActivity(myReviews);
	}

	// method home - returns the user to the home screen
	private void home() {
		Intent home = new Intent(this, MainActivity.class);
		startActivity(home);
	}
	// method reviewFood - new review screen
	private void reviewFood() {
		Intent reviewFood = new Intent(this, ReviewFoodActivity.class);
		startActivity(reviewFood);
	}
	
	/**
	 * Log out
	 * @param = Method logoutUser()
	 * @author Robert
	 * 
	 * Sets the SessionUser variables blank and re-loads/checks isLoggedIn().
	 * 
	 */
	//method logoutUser - logs user out of program.
	private void logoutUser() {

		// New user object
		User SessionUser = new User();

		// Create SessionUser global preference list
		final SharedPreferences SessionUserMgr = PreferenceManager.getDefaultSharedPreferences(context);

		// Set the values of SessionUser object to null values
		SessionUser.setFirstName("");
		SessionUser.setLastName("");
		SessionUser.setEmail("");
		SessionUser.setPassword("");

		// Set Global List to reflect current SessionUser object as logged out
		SharedPreferences.Editor edit = (SessionUserMgr).edit();
		edit.putString("First_Name", SessionUser.getFirstName());
		edit.putString("Last_Name", SessionUser.getLastName());
		edit.putString("Email", SessionUser.getEmail());
		edit.putString("Password", SessionUser.getPassword());
		edit.commit();

		// Re-load the page
		Intent logoutUser = new Intent(this, MainActivity.class);
		startActivity(logoutUser);

		// Give toast
		Toast.makeText(this, "Logged Out!", Toast.LENGTH_LONG).show();
	}

	/**
	 * popup method
	 * @param message
	 * 
	 * Creates a toast popup to display a message. 
	 * These popups are used during the scrum sprints to 
	 * display what the buttons will do when the project is complete.
	 */
	protected void popup(String message){
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}
}
